package org.blueoxygen.cimande2.ma.gene;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.blueoxygen.cimande2.ma.gene.entity.Person;
import org.blueoxygen.cimande2.ma.gene.entity.Preferences;
import org.blueoxygen.cimande2.ma.gene.manager.ConnectionManager;
import org.blueoxygen.cimande2.ma.gene.manager.DbManager;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;

public class DashBoard extends Activity {
	private ProgressDialog myProgressDialog;
	private Preferences preference;
	private ArrayList<Preferences> preferences;
	private DbManager dbManager;
	private ConnectionManager connectionManager;
	private String username;
	private static String SECRET_KEY = "";
	private String result;
	public Person person;
	private Menu menu;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.my);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.my_title);
		dbManager = new DbManager(this);
		connectionManager = new ConnectionManager();
		if (dbManager.getDataList().size() == 0) {
			Intent intent = new Intent(DashBoard.this, Preference.class);
			startActivity(intent);
		} else {

			preferences = dbManager.getDataList();
			connectionManager = new ConnectionManager();
			for (Preferences p : preferences) {
				preference = new Preferences(p.getUsername(), p.getPassword(),
						p.getConnection());
			}
			username = preference.getUsername();
			myProgressDialog = ProgressDialog.show(DashBoard.this, null,
					"Login in " + preference.getUsername() + "...", true);
			new Thread() {
				@Override
				public void run() {
					try {
						doLogin(preference.getUsername(), preference
								.getPassword());

						sleep(1500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					myProgressDialog.cancel();
				}
			}.start();

			
			findViewById(R.id.widget31).setOnClickListener(
					new OnClickListener() {
						public void onClick(View v) {
							Intent intent = new Intent(DashBoard.this, Parent.class);
							intent.putExtra("myprofile", person);
							startActivity(intent);
						}
					});
			findViewById(R.id.widget33).setOnClickListener(
					new OnClickListener() {
						public void onClick(View v) {
							Intent intent = new Intent(DashBoard.this, Profile.class);
							intent.putExtra("myprofile", person);
							startActivity(intent);
						}
					});
			findViewById(R.id.widget32).setOnClickListener(
					new OnClickListener() {
						public void onClick(View v) {
							Intent intent = new Intent(DashBoard.this, Child.class);
							intent.putExtra("myprofile", person);
							startActivity(intent);
						}
					});
		}
	}
	
	private String doLogin(String username, String password) {

		HttpClient httpclient = new DefaultHttpClient();
		Looper.prepare();
		HttpGet httpget = new HttpGet(dbManager.getConnectionUrl()
				+ "Cimande2/android/login?user=" + username + "&password="
				+ password);
		HttpResponse response;
		try {
			response = httpclient.execute(httpget);
			Log.i("Android JSON", response.getStatusLine().toString());
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();
				result = connectionManager.convertStreamToString(instream);

				if (result.equalsIgnoreCase("error")) {

				} else {
					SECRET_KEY = result.trim();
					setPerson(connectionManager.getPersonalData(dbManager
							.getConnectionUrl()
							+ "Cimande2/android/data.json?secret="
							+ SECRET_KEY
							+ "&user=" + preference.getUsername()));
				}
				instream.close();
			}

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == 1) {
			Intent in = new Intent(DashBoard.this, Preference.class);
			startActivity(in);
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		this.menu = menu;
		addRegularMenuItems(menu);
		return true;
	}

	private void addRegularMenuItems(Menu menu) {
		int base = Menu.FIRST; // value is 1
		MenuItem preference = menu.add(base, base, base, "Preference");
		preference.setIcon(R.drawable.wrench);
	}

}